@extends('admin.layouts.master')
@section('content')
    <table class="mdui-table mdui-table-hoverable">
        <thead>
        <tr>
            <th>ID</th>
            <th>管理员名</th>
            <th>角色名称</th>
            <th>是否允许</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach($users as $user)
        <tr>
            <td>{{$user->id}}</td>
            <td>{{$user->name}}</td>
            <td>{{$user->dname}}</td>

            <td>
                <label class="mdui-switch">
                    <input type="checkbox" is_allowed="{{$user->is_allowed}}"
                           class="change" {{$user->is_allowed==1?'checked':''}}/>
                    <i class="mdui-switch-icon"></i>
                </label>
            </td>

            <td>{{$user->created_at}}</td>

            <td>
                <a href="{{url('/admin/admin_user/edit',['id'=>$user->id])}}"
                   class="mdui-btn mdui-btn-dense mdui-color-blue mdui-ripple">修改</a>
                {{--删除--}}
                <a href="{{url('/admin/admin_user/destroy',['id'=>$user->id])}}"
                   class="mdui-btn mdui-btn-dense mdui-color-blue mdui-ripple"
                   onclick="if(confirm('确定删除吗?')==false) return false;">删除</a>
            </td>
        </tr>
            @endforeach
        </tbody>
    </table>
@endsection
@section('js')
<script>
    $(".change").click(function () {
        //获取每次选择得到的id
        var admin_id = $(this).parent().parent().parent().find('td').first().text();
        //增加一个标识
        var is_allowed = $(this).attr('is_allowed');
        //指定选定的ID
        var mythis = $(this);
        $.ajax({
            url: '{{url('/admin/admin_user/status')}}',
            dataType: 'json',
            type:'get',
            data: {'is_allowed': is_allowed, 'admin_id': admin_id},
            success: function (data) {
                if (data.message === 1) {
                    mythis.attr('checked', '');
                    mythis.attr('is_allowed', '1');
                } else {
                    mythis.removeAttr('checked');
                    mythis.attr('is_allowed', '0');
                }
            }
        });
    });
</script>
@endsection